/**
 *
 */
package com.ssyt.examinationsystem.dao.impl;

import com.jdbc.util.JdbcUtils;
import com.ssyt.examinationsystem.dao.IChoiceInfoDAO;
import com.ssyt.examinationsystem.entity.ChoiceInfo;
import com.ssyt.examinationsystem.entity.PageBean;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
public class ChoiceInfoDAOImpl implements IChoiceInfoDAO {

    /*
     * 添加选择题的实现类
     *
     * @see
     * com.ssyt.examinationsystem.dao.IChoiceInfoDAO#addChoiceInfo(com.ssyt.
     * examinationsystem.entity.ChoiceInfo)
     */
    @Override
    public int addChoiceInfo(ChoiceInfo choice) {
        String sql = "insert into ChoiceInfo values(null,?,?,?,?,?,?,?,?,?)";
        Object[] parameters = {choice.getQuestion(), choice.getAnswerA(), choice.getAnswerB(), choice.getAnswerC(),
                choice.getAnswerD(), choice.getAnswer(), choice.getChoiceType(), choice.getCourse().getCourseId(),
                choice.getRemark()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public List<Map<String, Object>> getSingles() {
        String sql = "select * from choiceInfo where choiceType=1";
        return JdbcUtils.executeQuery(sql);
    }

    @Override
    public List<Map<String, Object>> getMultiple() {
        String sql = "select * from choiceInfo where choiceType=2";
        return JdbcUtils.executeQuery(sql);
    }

    /*
     * (non-Javadoc)
     *
     * @see com.ssyt.examinationsystem.dao.IChoiceInfoDAO#getChoice()
     */
    @Override
    public List<Map<String, Object>> getChoice() {
        String sql = "select * from choiceInfo";
        return JdbcUtils.executeQuery(sql);
    }

    @Override
    public int updateChoice(ChoiceInfo choice) {
        String sql = "update choiceinfo set question=?,answerA=?,answerB=?,answerC=?,answerD=?,answer=?,"
                + "choiceType=?,courseId=?,remark=? where choiceId=?";
        Object[] parameters = {choice.getQuestion(), choice.getAnswerA(), choice.getAnswerB(), choice.getAnswerC(),
                choice.getAnswerD(), choice.getAnswer(), choice.getChoiceType(), choice.getCourse().getCourseId(),
                choice.getRemark(), choice.getChoiceId()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public int deleteChoice(String choiceId) {
        return JdbcUtils.executeUpdate("delete from choiceinfo where choiceId=?", choiceId);
    }

    @Override
    public List<Map<String, Object>> getChoiceByPage(PageBean page) {
        String sql = "select * from choiceinfo limit ?,?";
        // 获取每页显示记录数，当前页
        Integer pageSize = page.getPageSize();
        Integer currentPage = page.getCurrentPage();
        // 计算每页显示第一条记录索引
        Integer startIndex = (currentPage - 1) * pageSize;

        Object[] parameters = {startIndex, pageSize};
        return JdbcUtils.executeQuery(sql, parameters);
    }

    @Override
    public int getTotalCount() {
        String sql = "select * from choiceInfo";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql);
        return CollectionUtils.isEmptyForList(list) ? list.size() : 0;
    }

    @Override
    public Map<String, Object> getChoiceById(String choiceId, String courseId) {
        String sql = "select * from choiceInfo where choiceId= ? and courseId = ?";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, choiceId, courseId);

        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public List<Map<String, Object>> getRandomChoiceById(String number, String choiceType, String courseId) {
        String sql = "select * from choiceInfo where 1=1 and courseId = ?";
        if ("1".equals(choiceType)) {
            sql += " and choiceType=1";
        } else {
            sql += " and choiceType=2";
        }
        sql += " order by rand() limit ?";

        return JdbcUtils.executeQuery(sql, courseId, Integer.parseInt(number));
    }
}
